<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        /**
         * String字符串
         */ 
        var str = 'hello world'
        // var str = new String('hello world')

        // console.log(str.length)

        // console.log(str.charAt(1))
        // console.log(str.indexOf('o'))
        // console.log(str.lastIndexOf('o'))
        // console.log(str.toLowerCase())
        // console.log(str.toUpperCase())
        // console.log(str.substring(1, 3)) // 左闭右开
        // console.log(str.replaceAll('o', '888'))
        // console.log(str.split(' '))
        // console.log(str.trim())

        /**
         * Date日期
         */ 
        var today = new Date()
        // var d = new Date(2020, 10, 10, 10, 10, 10)
        // var d = new Date(1243524423);

        // console.log(today)
        var year = today.getFullYear()
        var month = checkNum(today.getMonth() + 1)
        var date = checkNum(today.getDate())
        var hour = checkNum(today.getHours())
        var minute = checkNum(today.getMinutes())
        var second = checkNum(today.getSeconds())
        var millisecond = today.getMilliseconds()
        var day = today.getDay()
        var weekday = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']

        // var currentTime = year+'-'+month+'-'+date+' '+hour+':'+minute+':'+second + ' ' + weekday[day]
        var currentTime = `${year}-${month}-${date} ${hour}:${minute}:${second} ${weekday[day]}`


        // 定义字符串的的三种方式 
        var name = '张三'
        var a = 'hello' + name
        var b = "hello"
        var c = `hello ${name}` // 称为模板字符串,可以使用占位符${}来引用变量

        
        console.log(currentTime)

        /**
         * 封装一个函数，实现数字小于10时前面补0
         */
        function checkNum(num){
            return num<10 ? '0'+num : num
        }


        /**
         * JSON基本语法
         */ 
        var user = {
            "name": "张三",
            "age": 20,
            "height": 190.3,
            "isMarried": true,
            "address": {
                "province": "江苏",
                "city": "南京",
                "district": "江宁"
            }
        }
        console.log(user)
        console.log(typeof user)
        console.log(user.name, user.height)
        console.log(user.address.city)

        /**
         * JSON转换
         */ 
        // 将JSON对象转换为字符串
        var str = JSON.stringify(user)
        console.log(str)
        console.log(typeof str)

        // 将字符中转换为JSON对象
        var s = '{"name":"李四","age":20,"height":190.3,"isMarried":true,"address":{"province":"江苏","city":"南京","district":"江宁"}}'
        var obj = JSON.parse(s)
        console.log(obj)
        console.log(obj.name)

        /**
         * JSON对象数组
         */ 
        var users = [
            {
                "id": 1001,
                "username": "admin",
                "password": "123",
                "status": 1
            },
            {
                "id": 1002,
                "username": "tom",
                "password": "123",
                "status": 0
            },
            {
                "id": 1003,
                "username": "alice",
                "password": "123",
                "status": 1
            }
        ]

        for(var u of users){
            console.log(u.id, u.username, u.password, u.status)
        }


        /**
         * 对象字面量
         */ 
        var stu = {
            name: 'tom',
            age: 20,
            study: function(){
                console.log('学习中....')
            },
            run: function(){
                console.log('跑步中...')
            }
        }
        console.log(stu.name, stu.age)
        stu.study()
        stu.run()



    </script>
</head>
<body>
    
</body>
</html>